package me.eg.util;

import java.util.LinkedList;
import java.util.Queue;

public class TreeNode {
    public int val = 0;
    public TreeNode left = null;
    public TreeNode right = null;

    public TreeNode(int val) {
        this.val = val;
    }

    public static TreeNode init(int[] arr) {
        if (arr == null || arr.length == 0)
            return null;
        TreeNode root = new TreeNode(arr[0]);
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        int index = 1;
        int len = arr.length;
        while (!queue.isEmpty()) {
            if (index >= len)
                break;

            TreeNode node = queue.poll();
            int tempVal = arr[index++];
            if (tempVal != -1) {
                TreeNode temp = new TreeNode(tempVal);
                node.left = temp;
                queue.offer(temp);
            }

            if (index >= len)
                break;
            tempVal = arr[index++];
            if (tempVal != -1) {
                TreeNode temp = new TreeNode(tempVal);
                node.right = temp;
                queue.offer(temp);
            }
        }
        return root;
    }
}
